package com.example.administrator.meishi;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.exception.BmobException;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.sms.listener.VerifySMSCodeListener;

public class Activity3 extends AppCompatActivity implements View.OnClickListener{
    //用户注册操作
    private EditText phone;
    private EditText et_mi;
    private EditText et_yz;
    private Button zc;
    private Button yz;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_3);
        initview();
        BmobSMS.initialize(this, "7168d73ba6ec89a9c83df9bac9fb2d19");
    }

    private void initview() {
        phone = (EditText)this.findViewById(R.id.et_phone);
        et_mi = (EditText)this.findViewById(R.id.et_mi);
        et_yz = (EditText)this.findViewById(R.id.ET_YZ);
        zc = (Button) findViewById(R.id.btn_ZC);
        yz = (Button) findViewById(R.id.btn_YZ);
        yz.setOnClickListener(this);
        zc.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        String phone = this.phone.getText().toString();
        String mima = et_mi.getText().toString();
        switch (v.getId()){
            case R.id.btn_YZ:
                if (phone.length()!=11){
                    Toast.makeText(this, "请输入11位有效手机号码", Toast.LENGTH_SHORT).show();
                }else {
                    BmobSMS.requestSMSCode(this, phone, "qwe", new RequestSMSCodeListener() {
                        @Override
                        public void done(Integer integer, BmobException e) {
                            if (e==null){
                                zc.setClickable(false);
                                zc.setBackgroundColor(Color.GRAY);
                                Toast.makeText(Activity3.this, "验证码发送成功，请尽快使用", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(Activity3.this, "验证码发送失败，请检查网络连接", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;
            case R.id.btn_ZC:
                if (phone.length()==0||mima.length()==0||phone.length()!=11){
                    Toast.makeText(this, "手机号或验证码输入不合法", Toast.LENGTH_SHORT).show();
                }else {
                    BmobSMS.verifySmsCode(this, phone, mima, new VerifySMSCodeListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e==null){
                                Toast.makeText(Activity3.this, "注册成功", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(Activity3.this, "验证码错误", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;

        }




    }
}





